package com.woniu.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.dto.ManagementDto;
import com.woniu.project.dto.ManagementRoleDto;
import com.woniu.project.dto.PermissionTypeAndPermissionDto;
import com.woniu.project.dto.RoleDto;
import com.woniu.project.params.*;
import com.woniu.project.service.ManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Klaus
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/pawn/management")
@Slf4j
@Api(tags = "Management 控制层")
public class ManagementController {
    @Autowired
    private ManagementService managementService;

    /**
     * 发送短信的服务
     * @param ManagementPhone 手机号码
     */
    @ApiOperation(value="发送短信的服务",notes="手机号是必输项")
    @PostMapping("sendMsg")
    public ResultDto sendMsg(String ManagementPhone){
        managementService.sendMsg(ManagementPhone);
        return ResultDto.success();
    }

    /**
     * 管理员登录 判断账号密码是否正确 短信验证码是否正确
     * @return
     */
    @ApiOperation(value="管理员登录",notes="判断账号密码是否正确 短信验证码是否正确")
    @PostMapping("login")
    public ResultDto login(@Valid @RequestBody ManagementParams managementParams){
        System.out.println("进入登录");
        String token = managementService.login(managementParams);
        System.out.println("登录完毕");
        return ResultDto.success(token);
    }


    /**
     * 新增管理员
     *
     * @param managementParams managementParams必须传入三个参数
     *                         managementName 用户名
     *                         managementPassword 密码
     *                         managementPhone 手机号
     * @return
     */
    @ApiOperation(value="新增管理员",notes="managementParams必须传入三个参数\n" +
            "     *                         managementName 用户名\n" +
            "     *                         managementPassword 密码\n" +
            "     *                         managementPhone 手机号")
    @PostMapping("addManagement")
    public ResultDto addManagement(@RequestBody ManagementParams managementParams){
        managementService.addManagement(managementParams);
        return ResultDto.success();
    }

    /**
     * 查询所有管理员
     * page:页数 默认值 1
     * size:每页条数 默认值 10
     * @param selectParam
     * @return
     */
    @ApiOperation(value="查询所有管理员",notes=" * page:页数 默认值 1 " +
            "* size:每页条数 默认值 10")
    @GetMapping("selectAll")
    public ResultDto selectAllManagement(SelectParam selectParam){
        IPage<ManagementDto> managementDtoIPage = managementService.selectAllManagement(selectParam);
        System.out.println(managementDtoIPage);
        return ResultDto.success(managementDtoIPage);
    }

    /**
     * 修改管理员状态
     * @param managementParams
     *managementId 管理员ID
     *managementStatus 修改后的管理员的状态
     * @return
     */
    @ApiOperation(value="修改管理员状态",notes="必填参数：     *managementId 管理员ID\n" +
            "     *managementStatus 修改后的管理员的状态")
    @PostMapping("changeManagementStatus")
    public ResultDto changeManagementStatus(ManagementParams managementParams){
        managementService.changeManagementStatus(managementParams);
        return ResultDto.success();
    }

    /**
     * 修改管理员的密码
     * @param managementParams
     * managementId 管理员ID
     * managementOldPassword  管理员旧密码
     * managementNewPassword  管理员新密码
     * @return
     */
    @ApiOperation(value="修改管理员的密码",notes="必填参数：     * managementId 管理员ID\n" +
            "     * managementOldPassword  管理员旧密码\n" +
            "     * managementNewPassword  管理员新密码 ")
    @PostMapping("changeManagementPassword")
    public ResultDto changeManagementPassword(ManagementChangePasswordParams managementParams){
        managementService.changeManagementPassword(managementParams);
        return ResultDto.success();
    }

    /**
     * 新增管理员的角色
     * @param roleParams
     * roleName 角色名
     * roleDescription 角色描述
     * @return
     */
    @ApiOperation(value="新增管理员的角色",notes="     * roleName 角色名\n" +
            "     * roleDescription 角色描述")
    @PostMapping("addRole")
    public ResultDto addRole(RoleParams roleParams){
        managementService.addRole(roleParams);
        return ResultDto.success();
    }

    /**
     * 给管理员分配角色
     * @return roleManagementParams
     * managementId 管理员ID
     * roleIdList 角色ID的集合
     */
    @ApiOperation(value="给管理员分配角色",notes="     * managementId 管理员ID\n" +
            "     * roleIdList 角色ID的集合")
    @PostMapping("allocationRole")
    public ResultDto allocationRole(@RequestBody RoleManagementParams roleManagementParams){
        managementService.allocationRole(roleManagementParams);
        return ResultDto.success();
    }

    /**
     * 查询所有管理员所有角色
     * @return
     */
    @ApiOperation(value="查询所有管理员所有角色",notes="无需参数")
    @GetMapping("selectAllManagementRole")
    public ResultDto selectAllManagementRole(){
        List<ManagementRoleDto> managementRoleDtos = managementService.selectAllManagementRole();
        return ResultDto.success(managementRoleDtos);
    }

    /**
     * 查询指定管理员所有角色
     * @param managementId
     * @return
     */
    @ApiOperation(value="查询指定管理员所有角色",notes="必填参数：managementId 管理员ID")
    @GetMapping("selectOneManagementRole")
    public ResultDto selectOneManagementRole(String managementId){
        System.out.println(managementId);
        List<RoleDto> roleDtos = managementService.selectOneManagementRole(managementId);
        return ResultDto.success(roleDtos);
    }

    /**
     * 查询权限列表
     * @return
     */
    @ApiOperation(value="查询权限列表",notes="无参数")
    @GetMapping("selectAllPermission")
    public ResultDto selectAllPermission(){
        List<PermissionTypeAndPermissionDto> permissionTypeAndPermissionDtos = managementService.selectAllPermission();
        return ResultDto.success(permissionTypeAndPermissionDtos);
    }

    /**
     * 根据角色ID修改角色的权限
     * @param rolePermissionParams
     * @return
     */
    @ApiOperation(value="根据角色ID修改角色的权限",notes="必填参数 ：roleId 角色ID，permissionIdList 权限集合")
    @PostMapping("changeRolePermission")
    public ResultDto changeRolePermission(@RequestBody RolePermissionParams rolePermissionParams){
        managementService.changeRolePermission(rolePermissionParams);
        return ResultDto.success();
    }

}

